package com.bdd.service.Impl;

import com.bdd.Enum.DiscountEnum;
import com.bdd.entity.Discount;
import com.bdd.mapper.ClothingDao;
import com.bdd.mapper.ClothingDiscountDao;
import com.bdd.mapper.DiscountDao;
import com.bdd.model.bo.DiscountBO;
import com.bdd.service.DiscountService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DiscountImpl implements DiscountService {

    @Autowired
    DiscountDao discountDao;

    @Autowired
    ClothingDiscountDao clothingDiscountDao;

    @Autowired
    ClothingDao clothingDao;
    @Override
    public Page<DiscountBO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<DiscountBO> discountBOList = discountDao.selectBOList();
//        for(DiscountBO discountBO : discountBOList){
//            discountBO.setTotalPrice(discountBO.getPrice()*
//                    DiscountEnum.getDiscountByName(discountBO.getDiscounts()));
//        }
        return discountBOList;
    }

    @Override
    public void add(DiscountBO bo) {
//        判断折扣名是否合理
        if(Objects.isNull(DiscountEnum.getDiscountByName(bo.getDiscounts()))){
            throw new IllegalArgumentException("请填写正确的折扣价");
        }
//        判断折扣名称是否存在
        if (!Objects.isNull(discountDao.selectByName(bo.getPromotionName()))){
            throw new IllegalArgumentException("折扣名称已存在，请编辑");
        }else {
            Discount newDiscount = Discount.builder()
                    .type("打折")
                    .name(bo.getPromotionName())
                    .discounts(bo.getDiscounts())
                    .startTime(bo.getStartTime())
                    .endTime(bo.getEndTime())
                    .build();
            if (new Date().before(bo.getStartTime())||new Date().after(bo.getEndTime())){
                newDiscount.setStatus(0);
            }else
                newDiscount.setStatus(1);
            discountDao.insert(newDiscount);
        }
////        如果折扣不存在则在discount插入新的折扣项
//        Integer discountId = null;
//        if (discountDao.selectDiscounts(bo.getDiscounts()) == null){
//            Discount newDiscount = Discount.builder()
//                    .type("打折")
//                    .discounts(bo.getDiscounts())
//                    .build();
//            discountDao.insert(newDiscount);
//            discountId = newDiscount.getId();
//        }else{
//            discountId = discountDao.selectDiscounts(bo.getDiscounts()).getId();
//        }
////        插入折扣对应表
//        ClothingDiscount clothingDiscount = ClothingDiscount.builder()
//                .clothingId(bo.getClothingId())
//                .discountId(discountId)
//                .build();
//        clothingDiscountDao.insert(clothingDiscount);

    }

    @Override
    public void edit(DiscountBO bo) {

        Discount discount = discountDao.selectByPrimaryKey(bo.getId());
        discount.setType("打折");
        discount.setName(bo.getPromotionName());
        discount.setDiscounts(bo.getDiscounts());
        discount.setStartTime(bo.getStartTime());
        discount.setEndTime(bo.getEndTime());
        if (new Date().before(bo.getStartTime())||new Date().after(bo.getEndTime())){
            discount.setStatus(0);
        }else
            discount.setStatus(1);
        discountDao.updateByPrimaryKey(discount);

//        //        更新折扣对应表
//        ClothingDiscount clothingDiscount = clothingDiscountDao.selectClothingDiscount(bo.getClothingId());
//        clothingDiscount.setDiscountId(discountID);
//        clothingDiscountDao.updateByPrimaryKey(clothingDiscount);
//        Integer discountID = bo.getId();
////        Discount discount = DisCountMapUtil.boTodo(bo);
////        discountDao.updateByPrimaryKey(discount);
//
////        如果折扣不存在则在discount插入新的折扣项
//        if (discountDao.selectDiscounts(bo.getDiscounts()) == null){
//            Discount newDiscount = Discount.builder()
//                    .type("打折")
//                    .discounts(bo.getDiscounts())
//                    .build();
//            discountDao.insert(newDiscount);
//            discountID = newDiscount.getId();
//        }

    }

    @Override
    public void delete(Integer id) {
//        clothingDiscountDao.deleteByClothingId(id);
        discountDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<DiscountBO> searchByName(String name) {
        List<DiscountBO> discountBOList = discountDao.selectBOListByName(name);
//        for(DiscountBO discountBO : discountBOList){
//            discountBO.setTotalPrice(discountBO.getPrice()*
//                    DiscountEnum.getDiscountByName(discountBO.getDiscounts()));
//        }
        return discountBOList;
    }


    static class DisCountMapUtil {

        static Discount boTodo(DiscountBO bo) {
            Discount discount = Discount.builder()
                    .id(bo.getId())
                    .discounts(bo.getDiscounts())
                    .type(bo.getType())
                    .build();
            return discount;
        }

        static DiscountBO doToBo(Discount discount) {
            DiscountBO bo = DiscountBO.builder()
                    .id(discount.getId())
                    .discounts(discount.getDiscounts())
                    .type(discount.getType())
                    .build();
            return bo;
        }

        static List<DiscountBO> doListToBoList(List<Discount> discountList) {
            List<DiscountBO> boList = discountList.stream().map(discount -> {
                DiscountBO bo = doToBo(discount);
                return bo;
            }).collect(Collectors.toList());
            return boList;
        }


    }
}
